package com.xyht.sca_s.student_manage_system.modules.salaryManagement.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.salaryManagement.entity.req.SmsSalaryTaskReq;
import com.xyht.sca_s.student_manage_system.modules.salaryManagement.service.SmsSalaryTaskService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 工资单发布任务表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-09-14
 */
@RestController
@RequestMapping("/salary/task")
@SecretBody
public class SmsSalaryTaskController {
    @Autowired
    private SmsSalaryTaskService smsSalaryTaskService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @PostMapping("/add")
    @ApiModelProperty(value = "工资单任务添加")
    public ResponseResult addSalaryTask(@RequestBody SmsSalaryTaskReq req, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsSalaryTaskService.addSalaryTask(req, user_id);
    }

    @GetMapping("/list")
    @ApiModelProperty(value = "工资单任务列表获取")
    public ResponseResult getSalaryTaskList(@RequestParam("search_str") String search_str,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            HttpServletRequest request
    ) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsSalaryTaskService.getSalaryTaskList(pageNum, pageSize, user_id, search_str);
    }

    @GetMapping("/detail")
    @ApiModelProperty(value = "工资单任务详情获取")
    public ResponseResult getSalaryTaskList(@RequestParam("task_id") String task_id) {
        return smsSalaryTaskService.getSalaryTaskDetail(task_id);
    }

    @PostMapping("/updateBasic")
    @ApiModelProperty(value = "工资单任务基础信息修改")
    public ResponseResult updateSalaryTaskBasicInfo(@RequestBody SmsSalaryTaskReq req, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsSalaryTaskService.updateSalaryTaskBasicInfo(req, user_id);
    }

    @PostMapping("/updateSetting")
    @ApiModelProperty(value = "工资单任务发布设置修改")
    public ResponseResult updateSalaryTaskPublishSetting(@RequestBody SmsSalaryTaskReq req, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsSalaryTaskService.updateSalaryTaskPublishSetting(req, user_id);
    }

    @PostMapping("/delete")
    @ApiModelProperty(value = "工资单任务删除")
    public ResponseResult deleteSalaryTask(@RequestBody SmsSalaryTaskReq req, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsSalaryTaskService.deleteSalaryTask(req, user_id);
    }

    @PostMapping("/check")
    @ApiModelProperty(value = "工资单核对数据")
    public ResponseResult checkSalaryDetailList(@RequestBody SmsSalaryTaskReq req) {
        return smsSalaryTaskService.checkSalaryDetailList(req);
    }

    @ApiModelProperty(value = "工资单任务导出")
    @PostMapping("/export")
    public ResponseResult exportExamineHistoryList(@RequestBody SmsSalaryTaskReq req, HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsSalaryTaskService.exportExamineHistoryList(req, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }


}

